package com.zch.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import com.item.service.MessageService;

import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;
import com.zch.dao.model.Activitymessage;
import com.zch.dao.model.Technologymessage;
import com.zch.dao.model.TechnologymessageExample;
import com.zch.dao.model.Zaiti;
import com.zch.dao.model.ZaitiExample;
import com.zch.dao.model.ZaitiType;
import com.zch.dao.model.ZxMessage;
import com.zch.daoEx.model.TechnologymessageEx;
import com.zch.daoEx.model.ZxMessageEx;
import com.zch.service.TechnologymessageService;

@RequestMapping("technologymessage")
@Controller
@LoginFilter
public class TechnologymessageController extends CoreController{

	@Autowired
	private TechnologymessageService technologymessageService;
	@Autowired
    private MessageService messageService;
	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows, String name){
        PaginationSupport.byPage(page, rows);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", name);
        List<TechnologymessageEx> list = technologymessageService.selectList(map);

        return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(Technologymessage technologymessage){
		if (StringUtils.isBlank(technologymessage.getId())){
			technologymessageService.insert(technologymessage);
		}else{
			technologymessageService.updateByPrimaryKeySelective(technologymessage);
			technologymessage = technologymessageService.selectByPrimaryKey(technologymessage.getId());
			if(technologymessage.getIsAgree()==2){
                String pushContent = "审核未通过!理由:"+technologymessage.getNotAgreeReason();
                //推送
                messageService.findMessage(technologymessage.getTechnologyName(), pushContent, technologymessage.getUserId(), 1);
            }
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		Technologymessage technologymessage = technologymessageService.selectByPrimaryKey(id);
		technologymessage.setContent(technologymessage.getContent().replaceAll("\r\n", "<br/>"));
		return JSONUtils.serialize(technologymessage);
	}
	
	@RequestMapping("/findByIds")
    @ResponseBody
    public String finds(String id){
        Technologymessage technologymessage = technologymessageService.selectByPrimaryKey(id);
        technologymessage.setContent(technologymessage.getContent().replaceAll("\r\n", "<br/>").replaceAll("\r", "<br/>").replaceAll("\n", "<br/>").replaceAll("\\s", "&nbsp;"));
        return JSONUtils.serialize(technologymessage);
    }

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			technologymessageService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}
    @RequestMapping("/update")
    @ResponseBody
    public String update(Technologymessage technologymessage){
        int result = technologymessageService.updateByPrimaryKeySelective(technologymessage);
        if(result == 1){
            technologymessage = technologymessageService.selectByPrimaryKey(technologymessage.getId());
            String pushContent = "";
            if(technologymessage.getIsAgree()==1){
                pushContent = "审核通过";
                //推送
                messageService.findMessage(technologymessage.getTechnologyName(), pushContent, technologymessage.getUserId(), 1);
            }
        }
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/myList")
    @ResponseBody
    public String myList(Integer page, Integer rows, String name){
        PaginationSupport.byPage(page, rows);
        String id = getSessionParameter("id");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("name", name);
        List<TechnologymessageEx> list = technologymessageService.selectMyList(map);

        return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
    }
    
    @RequestMapping("/myListSave")
    @ResponseBody
    public String myListSave(Technologymessage technologymessage){
        technologymessage.setUserId(getSessionParameter("id"));
        if(!getSessionParameter("sys").equals("inside")){
            return JSONUtils.serialize(new Ret(-1,"请重新登录..."));
        }
        if (StringUtils.isBlank(technologymessage.getId())){
            technologymessageService.insert(technologymessage);
        }else{
            technologymessageService.updateByPrimaryKeySelective(technologymessage);
        }
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/top")
    @ResponseBody
    public String top(Technologymessage technologymessage){
        if (StringUtils.isBlank(technologymessage.getId())){
            technologymessage.setPublishTime(new Date());
            technologymessageService.insert(technologymessage);
        }else{
            technologymessage.setPublishTime(new Date());
            technologymessageService.updateByPrimaryKeySelective(technologymessage);
        }
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/cancelTops")
    @ResponseBody
    public String cancelTops(Technologymessage technologymessage){
        if (StringUtils.isBlank(technologymessage.getId())){
            technologymessage.setPublishTime(null);
            technologymessageService.insert(technologymessage);
        }else{
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", technologymessage.getId());
            int result = technologymessageService.cancleTops(map);
        }
        return JSONUtils.serialize(new Ret(0));
    }
}